package com.kayden.blog.controller;

import com.kayden.blog.common.pojo.request.AddBlogRequest;
import com.kayden.blog.common.pojo.request.UpdateBlogRequest;
import com.kayden.blog.common.pojo.response.BlogInfoResponse;
import com.kayden.blog.service.BlogService;
import com.kayden.blog.service.impl.BlogServiceImpl;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/blog")
public class BlogController {

    @Resource(name = "blogServiceImpl")
    private BlogService blogService;
//    @Autowired
//    private BlogServiceImpl blogService;

    @RequestMapping("/getList")
    public List<BlogInfoResponse> getList(){
        log.info("获取博客列表....");
        List<BlogInfoResponse> blogInfos = blogService.getList();
        return blogInfos;

    }

    @RequestMapping("/getBlogDetail")
    public BlogInfoResponse getBlogDetail(@NotNull(message = "blogId不能为null")Integer blogId){
        log.info("获取博客详情，blogID:{}",blogId);
        return blogService.getBlogDetail(blogId);
    }


    @RequestMapping("/add")
    public Boolean addBlog(@RequestBody @Validated AddBlogRequest addBlogRequest){
        log.info("发布博客，userId:{},title:{}",addBlogRequest.getUserId(),addBlogRequest.getTitle());
        return blogService.addBlog(addBlogRequest);
    }

    @RequestMapping("/update")
    public Boolean updateBlog(@RequestBody @Validated UpdateBlogRequest updateBlogRequest){
        log.info("更新博客,request:{}",updateBlogRequest);
        return blogService.updateBlog(updateBlogRequest);
    }

    public Boolean delete(@NotNull(message = "id不能为空")Integer blogId){
        log.info("删除博客，id:{}",blogId);
        return blogService.deleteBlog(blogId);
    }


}
